package org.jetlinks.pro.network.coap;

import lombok.SneakyThrows;
import org.jetlinks.pro.network.security.Certificate;
import org.jetlinks.pro.network.security.DefaultCertificate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

import java.util.Collections;

public class CoapTestUtils {

    @SneakyThrows
    public static Certificate createServerCertificate(String id){
        DefaultCertificate serverCert = new DefaultCertificate(id, "test");
        byte[] serverKs = StreamUtils.copyToByteArray(new ClassPathResource("server.pem").getInputStream());
        byte[] serverCer = StreamUtils.copyToByteArray(new ClassPathResource("server.csr").getInputStream());

        byte[] trust = StreamUtils.copyToByteArray(new ClassPathResource("trustStore.pem").getInputStream());

        serverCert.initPemKey(Collections.singletonList(serverKs), Collections.singletonList(serverCer))
                  .initPemTrust(Collections.singletonList(trust));

        return serverCert;
    }


    @SneakyThrows
    public static Certificate createClientCertificate(String id){
        DefaultCertificate clientCert = new DefaultCertificate(id, "test");

        byte[] clientKs = StreamUtils.copyToByteArray(new ClassPathResource("client.pem").getInputStream());
        byte[] clientCer = StreamUtils.copyToByteArray(new ClassPathResource("client.csr").getInputStream());

        byte[] trust = StreamUtils.copyToByteArray(new ClassPathResource("trustStore.pem").getInputStream());

        clientCert.initPemKey(Collections.singletonList(clientKs), Collections.singletonList(clientCer))
                  .initPemTrust(Collections.singletonList(trust));

        return clientCert;
    }
}
